package com.shijie.controller.erp;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import tk.mybatis.mapper.entity.Condition;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shijie.core.Result;
import com.shijie.core.ResultGenerator;
import com.shijie.core.annotation.Log;
import com.shijie.core.annotation.RequiresPermissions;
import com.shijie.core.constants.PnConstant.BusinessType;
import com.shijie.domain.entity.ActivityProducts;
import com.shijie.domain.entity.Activitys;
import com.shijie.service.ActivitysService;

/*
 *  @author: smart boy
 *  @date: 2018-08-13
 */
@RestController
@RequestMapping("/erp/activitys")
public class ActivitysController {

    @Resource
	private ActivitysService activitysService;

    @RequiresPermissions(value={"activity:list"})
	@GetMapping
    public Result list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size,@RequestParam(required=false) String q) {
        PageHelper.startPage(page, size);
        Condition condition = new Condition(Activitys.class);
        if(!StringUtils.isEmpty(q)){
        	condition.createCriteria().andLike("name", "%"+q+"%");
        }
        List<Activitys> list = activitysService.selectByCondition(condition);
        PageInfo<Activitys> pageInfo = new PageInfo<>(list);
        return ResultGenerator.success(pageInfo);
	}

	@PostMapping
	 @Log(title = "优惠活动", action = BusinessType.INSERT)
	@RequiresPermissions(value={"activity:create","activity:edit"})
    public Result add(@RequestBody Activitys activitys) {
        activitysService.save(activitys);
        return ResultGenerator.success();
	}

	 @GetMapping("/active/{activeId}")
    public Result getProductByActivityId(@PathVariable Integer activeId) {
        List<ActivityProducts> activitys = activitysService.listProductByActiveId(activeId);
        return ResultGenerator.success(activitys);
	}
 
	 @Log(title = "优惠活动", action = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        activitysService.deleteById(id);
        return ResultGenerator.success();
	}

    @GetMapping("/{id}")
    public Result detail(@PathVariable Integer id) {
        Activitys activitys = activitysService.selectById(id);
        return ResultGenerator.success(activitys);
	}
}

